package com.neu.cpsp.controller;

import com.neu.cpsp.pojo.RegulationDetail;
import com.neu.cpsp.service.RegulationDetailService;
import com.neu.cpsp.util.result.Result;
import com.neu.cpsp.util.result.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/27 20:28
 */

@CrossOrigin
@RestController
@RequestMapping("/regulationdetail")
public class RegulationDetailController {

    @Autowired
    private RegulationDetailService regulationDetailService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@RequestParam Integer regulationTypeId,
                          @RequestParam String code,
                          @RequestParam String describe){
        RegulationDetail record = new RegulationDetail();
        record.setRegulationTypeId(regulationTypeId);
        record.setCode(code);
        record.setDescribe(describe);

        if (regulationDetailService.submitAllInfo(record))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("查找")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam Integer typeid) {
        if (typeid == null)
            return null;
        else
            return ResultGenerator.genSuccessResult(regulationDetailService.getByTypeId(pageSize, currentPage, typeid));
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("根据typeID以及描述查找")
    @RequestMapping(value = "/selectAll",method = RequestMethod.POST)
    @ResponseBody
    public Result selectAll(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam Integer typeid,
                         @RequestParam String describe) {
        if (typeid == null && describe==null)
            return null;
        else
            return ResultGenerator.genSuccessResult(regulationDetailService.getByTypeIdAndDescribe(pageSize,currentPage,typeid,describe));
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(regulationDetailService.delete(id));
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam Integer id,
                         @RequestParam Integer regulationTypeId,
                         @RequestParam String code,
                         @RequestParam String describe) {

        RegulationDetail record = new RegulationDetail();
        record.setId(id);
        record.setRegulationTypeId(regulationTypeId);
        record.setCode(code);
        record.setDescribe(describe);
        return ResultGenerator.genSuccessResult(regulationDetailService.update(record));
    }
}
